//
//  MyDocumentViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/5.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "MyDocumentViewModel.h"

#import "DocumentCellModel.h"

#import "DocumentsListData.h"

@interface MyDocumentViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation MyDocumentViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    self.cellHeight = 88.0f;
    
    self.lastPage = 100;
    self.shouldMultiSections = NO;
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    
    /// 选中
    //    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(ArticleItemViewModel * itemViewModel) {
    //        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    //            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
    //                [subscriber sendNext:itemViewModel];
    //                [subscriber sendCompleted];
    //            });
    //            return nil;
    //        }];
    //    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
}

- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page
{
    @weakify(self)
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        NSString* strUrl = url_interface_userlibrary;
        NSDictionary* postData = interface_param_userlibrary(um.userData.userID);
        
        NSString* curSearchText = self.searchText;
        if (curSearchText && [curSearchText isNotBlank])
        {
            strUrl = url_interface_articlesearch;
            postData = interface_param_articlesearch(curSearchText, @"", @"", um.userData.userID, @(page+1), @(List_PageSize));
        }
        
        @weakify(self);
        [[AFNetWorkUtils racPOSTWthURL:strUrl params:postData] subscribeNext:^(id x)
         {
             @strongify(self);
             
             DocumentsListData* data = [DocumentsListData modelWithJSON:x];
             
             @weakify(self);
             NSArray *viewModels = [data.datas.rac_sequence map:^DocumentCellModel *(DocumentListData* aListData)
                                    {
                                        @strongify(self);
                                        
                                        @weakify(self);
                                        DocumentCellModel *itemViewModel = [[DocumentCellModel alloc] initWithData:aListData cellHeight:self.cellHeight];
                                        
                                        return itemViewModel;
                                    }].array;
             
             if (self.shouldPullUpToLoadMore)
             {
                 if (0 == page)
                 {
                     self.dataSource = viewModels;
                 }
                 else
                 {
                     if (self.dataSource == nil)
                     {
                         self.dataSource = viewModels;
                     }
                     else
                     {
                         self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                     }
                 }
             }
             else
             {
                 self.dataSource = viewModels;
             }
             
             [subscriber sendNext:nil];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

@end
